package com.huangpuweb.task.controller;

import com.huangpuweb.task.Constants;
import com.huangpuweb.task.model.QzYwjkPassagerFlow;
import com.huangpuweb.task.model.SysHpScenicState;
import com.huangpuweb.task.model.SysRefinedCrowdSituation;
import com.huangpuweb.task.service.SyncQzHpCgzfiFibDataService;
import com.huangpuweb.task.service.SyncSysDataService;
import com.huangpuweb.task.util.DateUtil;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


/**
 * 同步客流监控平台数据-定时任务
 * 同步豫园外圈、田子坊、
 * 新天地、外滩客流数据
 */
@Controller
public class SyncDataFromKLJKJob {

    private static Logger logger = Logger.getLogger(SyncDataFromKLJKJob.class);

    private SyncQzHpCgzfiFibDataService syncQzHpCgzfiFibDataService;

    @Autowired
    public void setSyncQzHpCgzfiFibDataService(SyncQzHpCgzfiFibDataService syncQzHpCgzfiFibDataService) {
        this.syncQzHpCgzfiFibDataService = syncQzHpCgzfiFibDataService;
    }



    private SyncSysDataService syncSysDataService;
    @Autowired
    public void setSyncSysDataService(SyncSysDataService syncSysDataService) {
        this.syncSysDataService = syncSysDataService;
    }


    /**
     * 同步客流监控平台数据-定时任务
     * @throws Exception
     */
    public void syncKLJKDataTask() throws Exception {

        /**
         * 同步客流监控点数据到Sys_refined_crowdSituation表
         */
        LogFactory.getLog(this.getClass()).info("--------start-------syncKLJKDataTask----------"+new Date());
        String[] kljkspotcodeArr = Constants.KLJKSPOTCODE.split(",");
        QzYwjkPassagerFlow qzywjk=null;
        SysRefinedCrowdSituation sysrefinedcrowd=null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < kljkspotcodeArr.length; i++) {
            qzywjk=syncQzHpCgzfiFibDataService.getQzYwJkPassagerFlowBySpotid(kljkspotcodeArr[i]);
            if(qzywjk != null){
                sysrefinedcrowd=new SysRefinedCrowdSituation();
                sysrefinedcrowd.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                //设置数据创建时间
                sysrefinedcrowd.setCreateTime(sdf.format(qzywjk.getUpdatetimer()));
                //sysrefinedcrowd.setCreateTime();
                //设置实时人流
                sysrefinedcrowd.setRealtimeNumber(Integer.valueOf(qzywjk.getRealtimenumber()));
                //设置累计人流量
                sysrefinedcrowd.setTotalNumber(qzywjk.getTotalnumber());
                //设置数据更新时间
                Date date=new Date();
                sysrefinedcrowd.setUpdateTime(sdf.format(date));
                //设置景区编码和景区名称
                if (qzywjk.getSpotid().equalsIgnoreCase("yuyuan")){
                    sysrefinedcrowd.setSpotCode(Constants.YUYUAN_WAIQUANCODE);
                    sysrefinedcrowd.setSpotName(Constants.YUYUANWAIQUANNAME);
                }else if (qzywjk.getSpotid().equalsIgnoreCase("waitan")){
                    sysrefinedcrowd.setSpotCode(Constants.WAITANCODE);
                    sysrefinedcrowd.setSpotName(Constants.WAITANNAME);
                }else if (qzywjk.getSpotid().equalsIgnoreCase("xintiandi")){
                    sysrefinedcrowd.setSpotCode(Constants.XINTIANDICODE);
                    sysrefinedcrowd.setSpotName(Constants.XINTIANDINAME);
                }else{
                    sysrefinedcrowd.setSpotCode(Constants.TIANZIFANGCODE);
                    sysrefinedcrowd.setSpotName(Constants.TIANZIFANGNAME);
                }

                //设置数据来源
                sysrefinedcrowd.setDataSources(Constants.SOURCETWO);
                //设置对接形式
                sysrefinedcrowd.setRemarks("数据库对接");

                //插入数据
                syncSysDataService.insertSysRefinedCrowdSituation(sysrefinedcrowd);
                //插入数据到备份表中
                syncSysDataService.insertSysBackupCrowdSituation(sysrefinedcrowd);
            }



        }


        /**
         *清洗数据到hp_scenic_state表中
         */
        String[] spotCodeArr = Constants.SPOTCODELIST.split(",");

        //获取当前日期的0点和23点
        String todayZero = DateUtil.getTodayZero();
        String today23   =DateUtil.getToday23(23,59,59);

        SysRefinedCrowdSituation maxSysrefinedcrowd=null;
        SysRefinedCrowdSituation realSysrefinedcrowd=null;
        SysHpScenicState sysHpScenicState=null;

        for (int i = 0; i < spotCodeArr.length; i++) {

            //获取截至目前人流量峰值和时间,景区名称、景区编号
            maxSysrefinedcrowd=syncSysDataService.getMaxNumberBySysRefinedCrowdSituation(spotCodeArr[i],todayZero,today23);
            //System.out.println(maxSysrefinedcrowd.getRealtimeNumber());
            //获取最新的实施人流量和今日总人流量
            realSysrefinedcrowd=syncSysDataService.getRealNumberBySysRefinedCrowdSituation(spotCodeArr[i]);
            //System.out.println(realSysrefinedcrowd.getRealtimeNumber());

            sysHpScenicState=new SysHpScenicState();

            if(maxSysrefinedcrowd != null && realSysrefinedcrowd!=null){


                //设置景区名称
                sysHpScenicState.setSpotName(maxSysrefinedcrowd.getSpotName());
                //设置景区编码
                sysHpScenicState.setSpotCode(spotCodeArr[i]);
                //设置景区今日峰值
                sysHpScenicState.setMaxNumber(maxSysrefinedcrowd.getRealtimeNumber());
                //设置景区峰值产生时间
                sysHpScenicState.setMaxTime(maxSysrefinedcrowd.getCreateTime());
                //设置景区实时人流量
                int realtimeNumber = Integer.valueOf(realSysrefinedcrowd.getRealtimeNumber());
                sysHpScenicState.setRealTimeNumber(realtimeNumber);
                //设置景区总人流量
                sysHpScenicState.setTotalNumber(Integer.valueOf(realSysrefinedcrowd.getTotalNumber()));
                //设置景区面积 SPOTCODELIST
                String area="";
                if(spotCodeArr[i].equals("SHHPQ_YUYUAN_WAIQUAN")){
                    area="5";
                }else if(spotCodeArr[i].equals("SHHPQ_YUYUAN_NEIQUAN")){
                    area="2";
                }else if(spotCodeArr[i].equals("SHHPQ_WAITAN")){
                    area="2.22";
                }else if(spotCodeArr[i].equals("SHHPQ_NANJINGLU")){
                    area="3.2";
                }else if(spotCodeArr[i].equals("SHHPQ_TIANZIFANG")){
                    area="2.4";
                }else{
                    area="3";
                }

                sysHpScenicState.setSpotArea(area);
                //设置人群密度
                DecimalFormat df = new DecimalFormat("0.00");
                double _area = Double.valueOf(area) * 10000;
                String density = df.format(realtimeNumber/_area);
                sysHpScenicState.setPeopleDensity(density);
                //设置景区监控点
                //TODO
                //获取景区摄像头数量
                String spotmonitornumber="";
                if(spotCodeArr[i].equals("SHHPQ_YUYUAN_WAIQUAN")){
                    spotmonitornumber="0";
                }else if(spotCodeArr[i].equals("SHHPQ_YUYUAN_NEIQUAN")){
                    spotmonitornumber="0";
                }else if(spotCodeArr[i].equals("SHHPQ_WAITAN")){
                    spotmonitornumber="0";
                }else if(spotCodeArr[i].equals("SHHPQ_NANJINGLU")){
                    spotmonitornumber="77";
                }else if(spotCodeArr[i].equals("SHHPQ_TIANZIFANG")){
                    spotmonitornumber="0";
                }else{
                    spotmonitornumber="0";
                }

                int monitorNumber=0;
                if (null != spotmonitornumber) {
                    monitorNumber=Integer.valueOf(spotmonitornumber);
                }
                sysHpScenicState.setMonitorNumber(monitorNumber);
                //设置风险等级
                sysHpScenicState.setRiskLevel(realtimeNumber);
                sysHpScenicState.setRecordTime(DateUtil.getDateTime());
                //更新数据 hp_scenic_state
                syncSysDataService.updateHpScenicState(sysHpScenicState);
            }

        }




        LogFactory.getLog(this.getClass()).info("--------end-------syncKLJKDataTask----------"+new Date());





    }


}
